//
//  COMessageViewController.swift
//  CocoProject
//
//  Created by Ruthless on 2023/8/19.
//

import UIKit
import TZImagePickerController

class COMessageViewController: COBaseViewController {
    private var image: UIImage? {
        didSet {
            if let image = image {
                imageView.image = image
                deleteButton.isHidden = false
            } else {
                imageView.image = UIImage(named: "编组 13")
                deleteButton.isHidden = true
            }
        }
    }
    
    override var navShow: Bool {
        true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

    }
    
    override func configUI() {
        view.backgroundColor = .rgba(17, 7, 41)
        
        view.addSubview(imageView)
        imageView.addSubview(deleteButton)
        view.addSubview(ageLabel)
        view.addSubview(ageTextField)
        view.addSubview(nextButton)
        view.addSubview(skipButton)
        
        imageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.height.equalTo(145)
            make.top.equalToSuperview().offset(104+statusBarHeight)
        }
        
        deleteButton.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-8)
            make.top.equalToSuperview().offset(10)
        }
        
        ageLabel.snp.makeConstraints { make in
            make.top.equalTo(imageView.snp.bottom).offset(55)
            make.left.equalToSuperview().offset(21)
        }
        
        ageTextField.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(21)
            make.height.equalTo(50)
            make.top.equalTo(ageLabel.snp.bottom).offset(10)
        }
        
        nextButton.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(21)
            make.bottom.equalToSuperview().offset(-92-bottomSafeHeight)
            make.height.equalTo(50)
        }
        
        skipButton.snp.makeConstraints { make in
//            make.bottom.equalToSuperview().offset(-99-bottomSafeHeight)
            make.top.equalTo(nextButton.snp.bottom).offset(25)
            make.centerX.equalToSuperview()
        }
    }
    
    private lazy var imageView: UIImageView = {
        let imgV = UIImageView(image: UIImage(named: "编组 13"))
        imgV.contentMode = .scaleAspectFit
        imgV.layer.masksToBounds = true
        imgV.layer.cornerRadius = 10
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(selectImage))
        imgV.addGestureRecognizer(tap)
        imgV.isUserInteractionEnabled = true
        return imgV
    }()
    
    private lazy var deleteButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "编组 2"), for: .normal)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        btn.isHidden = true
        return btn
    }()
    
    private lazy var ageLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(14)
        label.textColor = .white
        label.text = "Age"
        return label
    }()
    
    private lazy var ageTextField: UITextField = {
        let tf = UITextField()
        tf.textType = .generic
        tf.textColor = .white
        tf.font = .regular(14)
        tf.layer.masksToBounds = true
        tf.layer.cornerRadius = 25
        tf.addPaddingLeft(25)
        tf.backgroundColor = .rgba(41, 32, 63)
        tf.keyboardType = .numberPad
        return tf
    }()
    
    private lazy var nextButton: UIButton = {
        let btn = UIButton()
        let bgLayer1 = CAGradientLayer()
        bgLayer1.colors = [UIColor.rgba(218, 67, 255).cgColor, UIColor.rgba(82, 28, 255).cgColor]
        bgLayer1.locations = [0, 1]
        bgLayer1.frame = CGRect(x: 0, y: 0, width: SCREENWIDTH-38, height: 50)
        bgLayer1.startPoint = CGPoint(x: 1, y: 0.56)
        bgLayer1.endPoint = CGPoint(x: 0.56, y: 0.56)
        btn.layer.insertSublayer(bgLayer1, at: 0)
        
        btn.setTitle("Next", for: .normal)
        btn.titleLabel?.font = .medium(14)
        btn.setTitleColor(.white, for: .normal)
        
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 25
        
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        return btn
    }()
    
    private lazy var skipButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("Skip", for: .normal)
        btn.setTitleColor(.white.withAlphaComponent(0.5), for: .normal)
        
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        return btn
    }()
}

extension COMessageViewController: TZImagePickerControllerDelegate {
    @objc
    private func clickButton(_ btn: UIButton) {
        if btn == deleteButton {
            self.image = nil
        } else if btn == nextButton {
            let vc = COLevelViewController()
            vc.image = image
            if let text = ageTextField.text, !text.isEmpty {
                vc.age = Int(text)
            }
            
            self.navigationController?.pushViewController(vc, animated: true)
        } else if btn == skipButton {
            self.navigationController?.popToRootViewController(animated: true)
        }
    }
    
    @objc
    private func selectImage() {
        let picker = TZImagePickerController(maxImagesCount: 1, delegate: self)!
        self.present(picker, animated: true)
    }
    
    func imagePickerController(_ picker: TZImagePickerController!, didFinishPickingPhotos photos: [UIImage]!, sourceAssets assets: [Any]!, isSelectOriginalPhoto: Bool, infos: [[AnyHashable : Any]]!) {
        self.image = photos.first
    }
}
